package com.wenjiang.controller;

import com.wenjiang.common.dto.AdminLoginParam;
import com.wenjiang.common.exception.TokenException;
import com.wenjiang.common.lang.Result;
import com.wenjiang.common.util.JWTUtil;
import com.wenjiang.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 后台登陆结构
 */

@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    LoginService loginService;

    /**
     * 后台系统登陆
     */
    @RequestMapping("/login")
    public  Object adminLogin(@RequestBody AdminLoginParam adminLoginParam){

        log.info("进入了登陆接口");

        Result result = loginService.adminLogin(adminLoginParam);

        return result;

    }

    /**
     * 登出接口
     */
    @RequestMapping("/logout")
    public Object logout(HttpServletRequest request,HttpServletResponse response){
        // 获取当前会话 进行登出
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null){
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }


        return Result.succ("登出成功");
    }

    /**
     * 后台登陆 获取用户信息
     */

    @RequestMapping("/info")
    public  Object info(@RequestHeader("token") String token){


            //获取用户信息
          Long userId= JWTUtil.getUserId(token);

        Result info = loginService.getInfo(userId);

        return  info;


    }


    /**
     * 获取全部路由信息
     */
    @RequestMapping("/routers")
    public  Object getRouters(){
        Result routers = loginService.getRouters();
        return routers;
    }






}
